package demo;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class HelloServlet extends HttpServlet {
	private  int num = 0;
	private Lock lock = new ReentrantLock();
	private int max = 0;
	private int total = 0;
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		try {
			System.out.println("+1 :"+ num);
			lock.lock();
				num++;
				total++;
				if(total % 20 == 0){
					new TotalShowThead(total).start();	//每20个打印一次
				}
				
				if(num > max){
					max = num;
					new MaxShowThead(max).start();
				}
			lock.unlock();
			
			Thread.sleep(2000);
			byte[] bytes = new byte[256];
			
			lock.lock();
				num--;
			lock.unlock();
			System.out.println("-1 :"+ num);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
